//
//  HomeshipJYAnotherViewController.m
//  shuiyun
//
//  Created by edz on 2017/9/6.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "HomeshipJYAnotherViewController.h"
#import "QGDetailViewController.h"
#import "HomeshipJYRootTableViewCell.h"

@interface HomeshipJYAnotherViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    
    NSString * type;


}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation HomeshipJYAnotherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpData];
    
    [self creationView];
    [self regeditCell];
    
    
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64 - 40);
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去掉分割线
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}


- (void)regeditCell{
    
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeshipJYRootTableViewCell" bundle:nil] forCellReuseIdentifier:@"HomeshipJYRootTableViewCell"];
    
    
}

-(void)setUpData{
    
    if (_orderStatus == 0) {
        type = @"求购";
    }else{
        
        type = @"出售";
        
    }
    
    
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;

    _dataArr = [NSMutableArray array];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    [productParams setValue:token forKey:@"token"];
    NSString * userid = [PublicFunction getConfigWithKey:@"userid"];
    [productParams setValue:userid forKey:@"userid"];
    

    [productParams setValue:type forKey:@"type"];
   
 
    [[MNetworkHandler sharedInstance]conURL:HomeDeal networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        NSLog(@"%@", returnData);
        NSLog(@"%@", productParams);
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            
            _dataArr = [returnData objectForKey:@"data"];
            NSLog(@"!!!!!!!!!!!!!!!%@", _dataArr);
            
            [self.tableView reloadData];
            
            [hud removeFromSuperview];
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            [hud removeFromSuperview];            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        [hud removeFromSuperview];        
    }];
    


}

#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

//section 不随tableView滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.tableView)
        
    {
        
        CGFloat sectionHeaderHeight = 64; //header高度
        
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            
        }
        
    }
    
    
}


#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.dataArr.count;
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //    NSMutableArray *a = [self.dict1[indexPath.row] valueForKey:@"shop"];
    //
    //    return (180 + 140*a.count);
    return 90;
    
}
#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeshipJYRootTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"HomeshipJYRootTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSDictionary * dict = self.dataArr[indexPath.row];
    
   
    cell.boatStyleLabel.text =  [NSString stringWithFormat:@"船舶类型:%@",[dict objectForKey:@"c_type"]];
    cell.boatWidthLabel.text = [NSString stringWithFormat:@"船宽:%@",[dict objectForKey:@"kuan"]];
    cell.boatDeepLabel.text = [NSString stringWithFormat:@"船深:%@",[dict objectForKey:@"shen"]];
    cell.boatDateLabel.text = [NSString stringWithFormat:@"建造日期:%@",[dict objectForKey:@"c_date"]];
    cell.boatPriceLabel.text = [NSString stringWithFormat:@"价格:%@",[dict objectForKey:@"price"]];
    cell.boatLengthLabel.text = [NSString stringWithFormat:@"船长:%@",[dict objectForKey:@"chang"]];
    return cell;
    
}


//点击方法跳转到详情页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section ==0) {
        QGDetailViewController *vc = [[QGDetailViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        
        NSDictionary * dict = self.dataArr[indexPath.row];
        
        vc.idStr = [dict objectForKey:@"id"];
        
        
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
